<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('course.unit.point.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button
              icon="Back"
              @click="router.push({name: 'module_education_course_unit_point_list', query: {course_id: dataForm.course_id, unit_id: dataForm.unit_id}})">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-card class="box-card" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('course.basic_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row>
                <el-col :span="4">
                  <el-form-item :label="i18n('course.unit.point.picture')" prop="picture">
                    <el-upload
                      class="avatar-uploader"
                      :action="$http.adornUrl('/file/picture')"
                      :show-file-list="false"
                      :headers="upload_headers"
                      :on-success="handlePictureSuccess"
                      :before-upload="beforePictureUpload">
                      <img v-if="dataForm.picture" :src="dataForm.picture" class="avatar-upload"/>
                      <el-icon v-else class="avatar-uploader-icon"><Plus/></el-icon>
                    </el-upload>
                  </el-form-item>
                </el-col>
                <el-col :span="20">
                  <el-row :gutter="20">
                    <el-col :span="8">
                      <el-form-item :label="i18n('course.unit.point.title')" prop="title">
                        <el-input
                          v-model="dataForm.title"
                          :placeholder="i18n('common.please_input') + i18n('course.unit.point.title')" clearable></el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row :gutter="20">
                    <el-col :span="8">
                      <el-form-item :label="i18n('course.type')" prop="type">
                        <el-select
                          v-model="dataForm.type"
                          :placeholder="i18n('common.please_select') + i18n('course.type')" clearable>
                          <el-option v-for="(v, k) in typeList" :label="v.text" :key="k" :value="v.value"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item :label="i18n('course.unit.point.time_length')" prop="time_length">
                        <el-time-picker
                          v-model="dataForm.time_length"
                          :teleported="false"
                          :placeholder="i18n('common.please_select') + i18n('course.time_length')"
                          clearable>
                        </el-time-picker>
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row :gutter="20">
                    <el-col :span="24">
                      <el-form-item class="form-width-textarea-800" :label="i18n('course.unit.point.description')" prop="description">
                        <el-input
                          type="textarea"
                          v-model="dataForm.description"
                          :rows="5"
                          show-word-limit
                          maxlength="500"
                          :placeholder="i18n('common.please_input') + i18n('course.unit.point.description')">
                        </el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row :gutter="20">
                    <el-col :span="8">
                      <el-form-item :label="i18n('common.sort')" prop="sort">
                        <el-input-number v-model="dataForm.sort" :min="0">
                        </el-input-number>
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row :gutter="20">
                    <el-col :span="8">
                      <el-form-item :label="i18n('common.is_hidden')" prop="is_hidden">
                        <el-switch
                          v-model="dataForm.is_hidden"
                          active-value="1"
                          :active-text="i18n('common.yes')"
                          inactive-value="2"
                          :inactive-text="i18n('common.no')">
                        </el-switch>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item :label="i18n('common.status')" prop="status">
                        <el-switch
                          v-model="dataForm.status"
                          active-value="1"
                          :active-text="i18n('common.enable')"
                          inactive-value="2"
                          :inactive-text="i18n('common.disable')">
                        </el-switch>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('course.unit.point.detail_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row :gutter="20">
                <el-col :span="24">
                  <el-form-item class="mavon" prop="content" :label="i18n('course.unit.point.content')">
                    <editor ref="editorRef" :content="dataForm.content"></editor>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('course.unit.point.resource_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row :gutter="20">
                <el-col :span="24">
                  <el-form-item class="resource" :label="i18n('course.unit.point.resource')" prop="url">
                    <el-upload
                      :action="$http.adornUrl('/file/attachment')"
                      list-type="picture"
                      :headers="upload_headers"
                      :on-preview="handleAttachmentDownload"
                      :on-remove="handleAttachmentRemove"
                      :on-success="handleAttachmentSuccess"
                      :file-list="attachmentList">
                      <el-button type="primary">{{ i18n('common.upload') }}</el-button>
                    </el-upload>
                    <el-dialog v-model="dataPreview.status">
                      <img :src="dataPreview.url" alt="" style="width:100%;height:100%;" />
                    </el-dialog>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <div class="text item">
              <el-form-item>
                <el-button
                  v-if="isAuth('module:education:course:unit:point:handle')"
                  type="primary"
                  @click="dataFormSubmit(formRef)">
                  {{ i18n('common.confirm') }}
                </el-button>
                <el-button @click="resetForm(formRef)">
                  {{ i18n('common.reset') }}
                </el-button>
              </el-form-item>
            </div>
          </el-card>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="course-unit-point-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import { useUpload } from "@/hooks/upload/useUpload"
  import Editor from '@/components/form/editor'

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('education/course/unit/point')

  const editorRef = ref()
  const typeList = ref([])
  const attachmentList = ref([])

  const dataForm = reactive({
    id: 0,
    course_id: 0,
    unit_id: 0,
    type: '',
    title: '',
    picture: '',
    description: '',
    content: '',
    time_length: '',
    sort: 0,
    is_hidden: '2',
    status: '1',
    url: [],
    filename: [],
  })

  const dataRule = reactive({
    picture: [{
      required: true,
      message: i18n('course.unit.rules.picture.require'),
      trigger: 'blur',
    }],
    title: [{
      required: true,
      message: i18n('course.unit.rules.title.require'),
      trigger: 'blur',
    }]
  })

  const dataPreview = reactive({
    status: false,
    url: '',
  })

  const {
    formRef,
    resetForm
  } = useForm(model.value, dataForm)

  const {
    upload_headers,
    handlePictureSuccess,
    beforePictureUpload,
    beforeAttachmentUpload,
    handleAttachmentDownload,
    handleAttachmentSuccess,
    handleAttachmentRemove,
  } = useUpload(dataForm, dataPreview)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({
            'course_id': dataForm.course_id
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.type = data.data.type.value + ''
            dataForm.title = data.data.title
            dataForm.description = data.data.description
            dataForm.picture = data.data.picture
            dataForm.sort = data.data.sort
            dataForm.is_hidden = data.data.is_hidden.value + ''
            dataForm.status = data.data.status.value + ''
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${model.value}/detail/data`),
              method: 'get',
              params: $http.adornParams({
                'course_id': dataForm.course_id,
                'unit_id': dataForm.unit_id,
                'point_id': dataForm.id,
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                if(data.data)
                {
                  dataForm.content = data.data.content
                }
              }
            })
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${model.value}/statistical/data`),
              method: 'get',
              params: $http.adornParams({
                'course_id': dataForm.course_id,
                'unit_id': dataForm.unit_id,
                'point_id': dataForm.id,
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                dataForm.time_length = data.data.time_length_text
              }
            })
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/education/course/unit/point/resource/data`),
              method: 'get',
              params: $http.adornParams({
                'course_id': dataForm.course_id,
                'unit_id': dataForm.unit_id,
                'point_id': dataForm.id,
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                attachmentList.value = data.data.list
                dataForm.url = data.data.url || []
                dataForm.filename = data.data.filename || []
              }
            })
          }
        })
      }
    })
  }

  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate((valid) => {
      if (valid) {
        $http({
          url: $http.adornUrl(`/${model.value}/handle`),
          method: 'post',
          data: $http.adornData({
            id: dataForm.id || undefined,
            course_id: dataForm.course_id,
            unit_id: dataForm.unit_id,
            type: dataForm.type,
            title: dataForm.title,
            description: dataForm.description,
            content: editorRef.value.data,
            picture: dataForm.picture,
            time_length: dataForm.time_length,
            url: dataForm.url,
            filename: dataForm.filename,
            sort: dataForm.sort,
            is_hidden: dataForm.is_hidden,
            status: dataForm.status,
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            router.push({name: 'module_education_course_unit_point_list', query: {course_id: dataForm.course_id, unit_id: dataForm.unit_id}});
          } else {
            $message.error(i18n(data.message))
          }
        })
      }
    })
  }

  const loadTypeList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'education_course_unit_point_type',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        typeList.value = data.data
      } else {
        $message.error(i18n(data.message))
      }
    })
  }

  onMounted(() => {
    dataForm.course_id = query.course_id
    dataForm.unit_id = query.unit_id

    init(formRef)

    loadTypeList()
  })
</script>

<style lang="scss" scoped>
  .admin_form_main .el-form-item{
    .el-input {
      width: 400px;
    }
    .el-select {
      width: 400px;
    }
    .el-input-number {
      width: 400px;
    }
    :deep(.el-date-editor) {
      width: 400px;

      .el-input__wrapper {
        width: 260px;
      }
    }
  }
  .form-width-textarea-800 {
    width: 800px;
  }
  .resource {
    width: 95% !important;
  }
  :deep(.el-scrollbar) {
    overflow: hidden !important;
    height: 100%;
  }

</style>
